package com.yunche.yunchebusiness.ui.activity.evaluate

import com.alibaba.fastjson.JSONObject
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.AreaEntity
import com.yunche.yunchebusiness.entity.ColorEntity
import com.yunche.yunchebusiness.entity.EvaluateEntity
import com.yunche.yunchebusiness.entity.EvaluateInfo
import com.yunche.yunchebusiness.mvp.contract.EvaluateContract
import com.yunche.yunchebusiness.mvp.presenter.EvaluatePresenter
import com.yunche.yunchebusiness.toRequestBody
import com.yunche.yunchebusiness.ui.view.YuncheToolBar
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_evaluate_result.*

class EvaluateResultActivity : BaseActivity(), EvaluateContract.View {
    override fun getColorSuccess(colorEntity: ColorEntity) {
    }

    override fun getAreaSuccess(areas: List<AreaEntity>) {
    }

    private val mPresenter by lazy {
        EvaluatePresenter()
    }

    var evaluateInfo: EvaluateInfo? = null

    override fun setLayout(): Int = R.layout.activity_evaluate_result

    override fun initView() {
        mPresenter.attachView(this)
    }

    override fun initData() {
        var bundle = intent.extras
        if (bundle != null) {

            evaluateInfo = bundle.getSerializable("evaluateInfo") as EvaluateInfo

            tv_brand_name.text = evaluateInfo?.brandName
            tv_series_name.text = evaluateInfo?.modelName

            var para = JSONObject()
            para["mileage"] = evaluateInfo?.mileage!!.toFloat() * 10000
            para["buyCarDate"] = evaluateInfo?.buyCarDate
            para["trimId"] = evaluateInfo?.modelCode
            para["cityId"] = evaluateInfo?.cityName

            mPresenter.getEvaluate(para.toJSONString().toRequestBody())

        } else {
            ToastyUtils.showError("数据有误")
            finish()
        }
    }

    override fun getEvaluateSuccess(evaluateEntity: EvaluateEntity) {
        tv_c2b_a_hight.text = evaluateEntity.c2BPrices.a.up
        tv_c2b_b_hight.text = evaluateEntity.c2BPrices.b.up
        tv_c2b_c_hight.text = evaluateEntity.c2BPrices.c.up

        tv_c2b_a_middle.text = evaluateEntity.c2BPrices.a.mid
        tv_c2b_b_middle.text = evaluateEntity.c2BPrices.b.mid
        tv_c2b_c_middle.text = evaluateEntity.c2BPrices.c.mid

        tv_c2b_a_low.text = evaluateEntity.c2BPrices.a.low
        tv_c2b_b_low.text = evaluateEntity.c2BPrices.b.low
        tv_c2b_c_low.text = evaluateEntity.c2BPrices.c.low

        tv_b2c_a_hight.text = evaluateEntity.b2CPrices.a.up
        tv_b2c_b_hight.text = evaluateEntity.b2CPrices.b.up
        tv_b2c_c_hight.text = evaluateEntity.b2CPrices.c.up

        tv_b2c_a_middle.text = evaluateEntity.c2BPrices.a.mid
        tv_b2c_b_middle.text = evaluateEntity.c2BPrices.b.mid
        tv_b2c_c_middle.text = evaluateEntity.c2BPrices.b.mid

        tv_b2c_a_low.text = evaluateEntity.c2BPrices.a.low
        tv_b2c_b_low.text = evaluateEntity.c2BPrices.b.low
        tv_b2c_c_low.text = evaluateEntity.c2BPrices.b.low

        tv_new_price.text = evaluateEntity.ncmsrp + "万元"

        var para = JSONObject()
        para["bandCode"] = evaluateInfo?.bandCode
        para["maxEvaluate"] = evaluateEntity.b2CPrices.a.up
        para["minEvaluate"] = evaluateEntity.b2CPrices.c.low
        para["modelCode"] = evaluateInfo?.modelCode
        para["seriesCode"] = evaluateInfo?.seriesCode
        para["cityName"] = evaluateInfo?.cityName
        para["mileage"] = evaluateInfo?.mileage!!.toFloat() * 10000
        para["buyCarDate"] = evaluateInfo?.buyCarDate
        if (evaluateInfo?.id != -1) {
            para["id"] = evaluateInfo?.id
        }
        mPresenter.saveEvaluate(para.toJSONString().toRequestBody())

    }

    override fun saveEvaluateSuccess() {
        // ToastyUtils.showInfo("提交成功")
    }

    override fun showLoading() {
        CommonDialog.showLoading(mContext!!, Constant.Loading)
    }

    override fun hideLoading() {
        CommonDialog.cancle()
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun onBackPressed() {
        setResult(Constant.REFRESH_DATA)
        finish()
    }

    override fun initEvent() {
        toolbar.yuncheToolbarCallBack = object : YuncheToolBar.YuncheToolbarCallBack {
            override fun onBackClick() {
                onBackPressed()
            }

            override fun onRightClick() {
            }
        }
    }
}
